package cn.imvc.app.models;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Set;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.security.access.ConfigAttribute;
import org.springframework.security.web.util.matcher.RequestMatcher;
import org.springframework.stereotype.Service;

import cn.imvc.app.entities.AttrClass;
import cn.imvc.app.entities.GoodsClass;
import cn.imvc.app.repositories.GoodsClassRepository;
import cn.imvc.core.HttpContext;
import cn.imvc.core.ICache;
import cn.imvc.core.ModelUtil;
import cn.imvc.data.domain.ViewRequest;
/**
 * 
 * @author zhy
 * @date 2016年7月16日下午5:15:46
 *
 */
@Service
public class GoodsClassModelImpl implements GoodsClassModel{

	@Autowired
	private GoodsClassRepository goodsClassRepository ;
	@Autowired
	RefRelationModel refRelationModel;
	@Autowired
	ICache cache;
	
	public LinkedHashMap<RequestMatcher, Collection<ConfigAttribute>> getRequestMap() {

		return null;
	}

	public Page<GoodsClass> find(ViewRequest viewRequest) {
		
		return goodsClassRepository.findAll(ModelUtil.OrderByIdDesc(viewRequest));
	}

	public GoodsClass get(int id) {
		
		return goodsClassRepository.findOne(id);
	}

	public GoodsClass save(int id, GoodsClass goodsClass) {
		goodsClass.setParent(get(id));
		cache.removeSecond("MAX_ID", "goodsClass");
		GoodsClass goodsClassTmp = goodsClassRepository.saveAndFlush(goodsClass);
		return goodsClassTmp;
	}

	public GoodsClass update(int classId, GoodsClass resource) {
		GoodsClass gc = goodsClassRepository.getOne(classId);
		BeanUtils.copyProperties(resource, gc, new String[]{"id","parent"});
		return goodsClassRepository.saveAndFlush(gc);
	}

	public void delete(GoodsClass goodsClass) {
		
			goodsClassRepository.delete(goodsClass);
		
	}
	
	public String delete(int id){
		String result = refRelationModel.deleteDateCheck("Goods_Class", "id", id);
		if(result.equals("")){
			goodsClassRepository.delete(id);
			return "";
		}else{
			return result;
		}
	}
	
	public void deleteBatch(int[] ids){

	}

    public String getGoodsClassTree() {
		String rootUrl = HttpContext.getContext().getAttribute("rootUrl").toString();
		List<GoodsClass> goodsClassSet = goodsClassRepository.findByParentIsNullOrderBySortAsc();
		StringBuffer tree = new StringBuffer();
		for (GoodsClass goodsClass : goodsClassSet) {
			tree.append("<li class='treeli' data_id='" + goodsClass.getId() + "'><a href='" + rootUrl + "/goods/goods-class/" + goodsClass.getId() + "/edit' target='ajax' rel='jbsxBoxs'>"
					+ goodsClass.getName() + "</a>");
			tree.append(this.getChildGoodsClass(goodsClass.getId(),rootUrl));
			tree.append("</li>");
		}
		if(tree.toString().equals("")){
			tree.append("<li class='treeli' data_id='0'><a href='" + rootUrl + "/goods/goods-class/0/edit' target='ajax' rel='jbsxBoxs'>商品类别</a></li>");
		}	
		return tree.toString();
	}

	private Object getChildGoodsClass(int id, String rootUrl) {
		List<GoodsClass> childGoodsClasss = goodsClassRepository.findByParentIdOrderBySortAsc(id);
		StringBuffer childNode = new StringBuffer();
		if(childGoodsClasss.size()>0){
			childNode.append("<ul>");
			for (GoodsClass goodsClass : childGoodsClasss) {
				childNode.append("<li class='treeli' data_id='" + goodsClass.getId() + "'><a href='" + rootUrl + "/goods/goods-class/" + goodsClass.getId() + "/edit' target='ajax' rel='jbsxBoxs'>"
						+ goodsClass.getName() + "</a>");
				childNode.append(this.getChildGoodsClass(goodsClass.getId(), rootUrl));
			    childNode.append("</li>");
			}
			childNode.append("</ul>");
		}
		return childNode.toString();
	}

	public String treeLookup() {
		Set<GoodsClass> goodsClassSet = goodsClassRepository.findByParentIsNull();
		StringBuffer tree = new StringBuffer();
		for (GoodsClass goodsClass : goodsClassSet) {
			tree.append("<li><a href=\"javascript:\" onclick=\"$.bringBack({id:'"+goodsClass.getId()+"', name:'"+goodsClass.getName()+"'})\">"+goodsClass.getName()+"</a>");
			tree.append(this.getChildGC(goodsClass.getId(),goodsClass));
			tree.append("</li>");
		}
		return tree.toString();
	}

	private Object getChildGC(int id, GoodsClass goodsClass) {
		Set<GoodsClass> childGoodsClasss = goodsClassRepository.findByParentId(id);
		StringBuffer childNode = new StringBuffer();
		if(childGoodsClasss.size()>0){
			childNode.append("<ul>");
		  for (GoodsClass gc : childGoodsClasss) {
			  childNode.append("<li><a href=\"javascript:\" onclick=\"$.bringBack({id:'"+gc.getId()+"', name:'"+gc.getName()+"'})\">"+gc.getName()+"</a>");
		      childNode.append(this.getChildGC(gc.getId(), gc));
		      childNode.append("</li>");
		  }
		  childNode.append("</ul>");
		}
		return childNode.toString();
	}

	public List<GoodsClass> getClassPath(int goodsClassId){
		List<GoodsClass> classPathList = new ArrayList<GoodsClass>();
		List<GoodsClass> goodsClasses = goodsClassRepository.findAll();
		
		Iterator<GoodsClass> iterator = goodsClasses.iterator();
		GoodsClass sonClass = null;
		while (iterator.hasNext()) {
			GoodsClass goodsClass = (GoodsClass) iterator.next();
			if(goodsClassId == goodsClass.getId()){
				sonClass = goodsClass;
			}
		}
		classPathList.add(sonClass);
		return loopPath(classPathList,sonClass,goodsClasses);
	}
	
	private List<GoodsClass> loopPath(List<GoodsClass> classPathList,GoodsClass sonClass,List<GoodsClass> goodsClasses){
		Iterator<GoodsClass> iterator = goodsClasses.iterator();
		List<GoodsClass> classPathListUse = new ArrayList<GoodsClass>();
		while (iterator.hasNext()) {
			GoodsClass goodsClass = (GoodsClass) iterator.next();
			if(sonClass.getParent() != null && sonClass.getParent().getId() == goodsClass.getId()){
				classPathListUse.add(goodsClass);
				classPathListUse.addAll(classPathList);
				return loopPath(classPathListUse,goodsClass,goodsClasses);
			}
		}
		return classPathList;
	}

	@Override
	public String getClassPathStr(int goodsClassId) {
		List<GoodsClass> classPathList = getClassPath(goodsClassId);
		StringBuffer sb = new StringBuffer();
		Iterator<GoodsClass> iterator = classPathList.iterator();
		while (iterator.hasNext()) {
			GoodsClass goodsClass = (GoodsClass) iterator.next();
			sb.append(goodsClass.getId());
			if(iterator.hasNext()){
				sb.append(" ");
			}
		}
		return sb.toString();
	}


}
